<?php

namespace app\middleware;

use Closure;

//use app\libs\Auth;
use app\facade\Auth;
use think\facade\Lang;
use think\facade\Env;

class Localization
{

    public function handle($request, Closure $next)
    {
        $admin = Auth::guard('admin');
        if ($admin) {
            if (isset($admin['lang']) && $admin['lang']) {
                // 设置语言

                $langSet = $admin['lang'];
                $module = app('http')->getName();
                $controller = $request->controller();

                // 加载应用公共语言包 及 验证语言包
                Lang::load([
                    app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . 'common.php',
                    app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . 'validation.php',
                ]);

                // 加载对应控制器语言包
                Lang::load([
                    app_path() . 'lang' . DIRECTORY_SEPARATOR . $langSet . DIRECTORY_SEPARATOR . strtolower($controller) . '.php',
                ]);
            }
        }

        return $next($request);
    }
}
